module namespace kartdata="http://sendmeafilter.com/common/kartdata";

declare boundary-space preserve;
declare option xdmp:mapping "false";

declare function kartdata:emptyorder($newid) { 
	<order>
		<orderid>{$newid}</orderid>
		<orderState>newOrder</orderState>
		<kart>
			<orderid>{$newid}</orderid>
		    <frequency>3</frequency>
		    <starting>0</starting>
		    <state>UT</state>
		    <plan>monthly</plan>
		    <offerCode/>
		    <addItemForm>
		      <qty>1</qty>
		      <height>16</height>
		      <width>20</width>
		      <thickness>1</thickness>
		    </addItemForm>
	 	</kart>
	</order>
};
  
declare variable $order := 
<order>
	<orderid>HKZZ65PZZM2HK</orderid>
	<xitem id="HK7Y65PLLM2" qty="1" type="M8-1-D" height="16" width="20" thickness="1">
		<type>M8-1-D</type>
		<search>16x20 16x20x1</search>
	</xitem>
<kart>
    <id>empty</id>
    <frequency>3</frequency>
    <starting>0</starting>
    <state>UT</state>
    <plan>monthly</plan>
    <offerCode/>
    <addItemForm>
      <qty>1</qty>
      <height>16</height>
      <width>20</width>
      <thickness>1</thickness>
      <type>M8-1-D</type>
    </addItemForm>
    <xlineItem>
      <id>HK7Y65PLLM2</id>
      <class>kart-item</class>
      <qty>1</qty>
      <desc>Merv 8 Common 16x20x1</desc>
      <price>24.95</price>
      <extprice>24.95</extprice>
    </xlineItem>
    <xlineItem>
      <class>kart-discount</class>
      <desc>Discount</desc>
      <extprice>-4.15</extprice>
    </xlineItem>
    <lineItem>
      <class>kart-totals</class>
      <desc>Subtotal</desc>
      <extprice>0.00</extprice>
    </lineItem>
    <lineItem>
      <class>kart-final-total</class>
      <desc>Total</desc>
      <extprice>0.00</extprice>
    </lineItem>
 </kart>
</order>;
  
declare variable $pricing := 
<pricing>
  <taxmap>
    <tax state="UT" percent="6.85"/>
  </taxmap>
  <multiFilterDiscount amount="-5.00" perAdditional="true"/>
  <discounts>
    <discount code="family" title="Family discount (20% off)" percent="20.0"/>
    <discount code="megaplex" title="Megaplex customer discount (5% off)" percent="5.0"/>
  </discounts>
  <filters>
    <!-- M8 -->
    <type id="M8-1">
      <filter code="M8-1" name="Merv 8 Common" thickness="1" price="19.95">
        <sizes>10x10 10x20 12x20 14x20 15x20 16x20</sizes>
      </filter>
      <filter code="M8-1-S" name="Merv 8 Standard" thickness="1" price="24.95">
        <sizes>12x24 14x25 16x25 18x20 18x24 18x25 20x20 20x24 20x25 24x24</sizes>
      </filter>
      <filter code="M8-1-CA" name="Merv 8 Custom A" thickness="1" price="29.95" >
        <size sideUnder="30" fromSqInches="36" toSqInches="750"/>
      </filter>
      <filter code="M8-1-CB" name="Merv 8 Custom B" thickness="1" price="34.95">
        <size sideUnder="60" fromSqInches="180" toSqInches="1500"/>
      </filter>
    </type>
    <type id="M8-2">
      <filter code="M8-2-S" name="Merv 8 Standard" thickness="2" price="27.45">
        <sizes>10x20 12x20 12x24 14x20 14x25 15x20 16x20 16x24 16x25 18x20 18x24 18x25 20x20 20x24 20x25</sizes>
      </filter>
      <filter code="M8-2-CA" name="Merv 8 Custom A" thickness="2" price="32.95">
        <size sideUnder="30" fromSqInches="36" toSqInches="750"/>
      </filter>
      <filter code="M8-2-CB" name="Merv 8 Custom B" thickness="2" price="39.84">
        <size sideUnder="60" fromSqInches="180" toSqInches="1500"/>
      </filter>
    </type>
    <type id="M8-4">
      <filter code="M8-4-S" name="Merv 8 Standard" thickness="4" price="29.94">
        <sizes>12x24 16x20 16x25 18x24 20x20 20x24 20x25 24x24</sizes>
      </filter>
      <filter code="M8-4-CA" name="Merv 8 Custom A" thickness="4" price="35.94">
        <size sideUnder="30" fromSqInches="36" toSqInches="750"/>
      </filter>
      <filter code="M8-4-CB" name="Merv 8 Custom B" thickness="4" price="48.93">
        <size sideUnder="60" fromSqInches="180" toSqInches="1500"/>
      </filter>
    </type>
    <type id="M8-5">
      <filter code="M8-5-S" name="Merv 8 Standard" thickness="5" price="39.95">
        <sizes>20x20 20x25 16x25</sizes>
      </filter>
    </type>
    <!-- M11 -->
    <type id="M11-1">
      <filter code="M11-1-S" name="Merv 11 Standard" thickness="1" price="29.95">
        <sizes>10x10 10x20 12x20 12x24 14x20 14x25 15x20 16x20 16x25 18x24 18x25 20x20 20x24 20x25 20x30 24x24 25x25</sizes>
      </filter>
      <filter code="M11-1-CA" name="Merv 11 Custom A" thickness="1" price="34.95">
        <size sideUnder="30" fromSqInches="36" toSqInches="750"/>
      </filter>
      <filter code="M11-1-CB" name="Merv 11 Custom B" thickness="1" price="39.95">
        <size sideUnder="60" fromSqInches="180" toSqInches="1500"/>
      </filter>
    </type>
    <type id="M11-2">
      <filter code="M11-2-S" name="Merv 11 Standard" thickness="2" price="32.95">
        <sizes>10x20 12x20 12x24 14x20 14x25 15x20 16x20 16x24 16x25 18x24 18x25 20x20 20x24 20x25 24x24</sizes>
      </filter>
      <filter code="M11-2-CA" name="Merv 11 Custom A" thickness="2" price="38.45">
        <size sideUnder="30" fromSqInches="36" toSqInches="750"/>
      </filter>
      <filter code="M11-2-CB" name="Merv 11 Custom B" thickness="2" price="47.94">
        <size sideUnder="60" fromSqInches="180" toSqInches="1500"/>
      </filter>
    </type>
    <type id="M11-4">
      <filter code="M11-4-S" name="Merv 11 Standard" thickness="4" price="35.94">
        <sizes>12x24 16x20 16x25 18x24 20x20 20x24 20x25 24x24</sizes>
      </filter>
      <filter code="M11-4-CA" name="Merv 11 Custom A" thickness="4" price="41.94">
        <size sideUnder="30" fromSqInches="36" toSqInches="750"/>
      </filter>
      <filter code="M11-4-CA" name="Merv 11 Custom B" thickness="4" price="57.93">
        <size sideUnder="60" fromSqInches="180" toSqInches="1500"/>
      </filter>
    </type>
    <type id="M11-5">
      <filter code="M11-5-S" name="Merv 11 Standard" thickness="5" price="49.95">
        <sizes>20x20 20x25 16x25</sizes>
      </filter>
    </type>
    <!-- Carbon -->
    <type id="C-1">
      <filter code="C-1-1-S" name="Carbon Standard" thickness="1" price="39.95">
        <sizes>10x10 10x20 12x12 12x20 12x24 14x24 14x20 14x25 15x20 16x20 16x25 18x24 18x25 20x20 20x24 20x25</sizes>
      </filter>
      <filter code="C-1-2-S" name="Carbon Standard" thickness="1" price="44.95">
        <sizes>10x24 14x14 16x16 16x24 18x18 18x20 20x22 22x22 24x24 25x25</sizes>
      </filter>
      <filter code="C-1-3-S" name="Carbon Standard" thickness="1" price="49.95">
        <sizes>10x30 12x30 14x30 15x30 16x30 18x30 20x30 24x30</sizes>
      </filter>
      <filter code="C-1-CA" name="Carbon Custom A" thickness="1" price="54.95">
        <size sideUnder="30" fromSqInches="150" toSqInches="399"/>
      </filter>
      <filter code="C-1-CB" name="Carbon Custom B" thickness="1" price="64.95">
        <size sideUnder="30" fromSqInches="400" toSqInches="900"/>
      </filter>
      <filter code="C-1-CC" name="Carbon Custom C" thickness="1" price="69.95">
        <size sideUnder="60" fromSqInches="150" toSqInches="399"/>
      </filter>
      <filter code="C-1-CD" name="Carbon Custom D" thickness="1" price="74.95">
        <size sideUnder="60" fromSqInches="400" toSqInches="900"/>
      </filter>
    </type>
    <type id="C-2">
      <filter code="C-2-1-S" name="Carbon Standard" thickness="2" price="43.95">
        <sizes>10x20 12x12 12x20 12x24 14x20 14x25 15x20 16x20 16x25 18x20 18x24 18x25 20x20 20x24 20x25</sizes>
      </filter>
      <filter code="C-2-2-S" name="Carbon Standard" thickness="2" price="49.45">
        <sizes>16x16 16x24 18x18 18x20 24x24 25x25</sizes>
      </filter>
      <filter code="C-2-3-S" name="Carbon Standard" thickness="2" price="54.95">
        <sizes>20x30</sizes>
      </filter>
      <filter code="C-2-CA" name="Carbon Custom A" thickness="2" price="60.45">
        <size sideUnder="30" fromSqInches="150" toSqInches="399"/>
      </filter>
      <filter code="C-2-CB" name="Carbon Custom B" thickness="2" price="71.45">
        <size sideUnder="30" fromSqInches="400" toSqInches="900"/>
      </filter>
      <filter code="C-2-CC" name="Carbon Custom C" thickness="2" price="76.95">
        <size sideUnder="60" fromSqInches="150" toSqInches="399"/>
      </filter>
      <filter code="C-2-CD" name="Carbon Custom D" thickness="2" price="82.45">
        <size sideUnder="60" fromSqInches="400" toSqInches="900"/>
      </filter>
    </type>
    <type id="C-4">
      <filter code="C-4-1-S" name="Carbon Standard" thickness="4" price="47.94">
        <sizes>12x24 16x20 16x25 18x24 20x20 20x24 20x25 24x24</sizes>
      </filter>
      <filter code="C-4-CA" name="Carbon Custom A" thickness="4" price="65.94">
        <size sideUnder="30" fromSqInches="150" toSqInches="399"/>
      </filter>
      <filter code="C-4-CB" name="Carbon Custom B" thickness="4" price="77.94">
        <size sideUnder="30" fromSqInches="400" toSqInches="900"/>
      </filter>
      <filter code="C-4-CC" name="Carbon Custom C" thickness="4" price="83.94">
        <size sideUnder="60" fromSqInches="150" toSqInches="399"/>
      </filter>
      <filter code="C-4-CD" name="Carbon Custom D" thickness="4" price="104.93">
        <size sideUnder="60" fromSqInches="400" toSqInches="900"/>
      </filter>
    </type>
    <type id="C-5">
      <filter code="C-5-S" name="Carbon Standard" thickness="5" price="59.95">
        <sizes>20x20 20x25 16x25</sizes>
      </filter>
    </type>
  </filters>
</pricing>
;

	(:
	let $subtotal = 0;
	let $totalFilters = 0;
	let $arLen=cart.item.length;
	for ( let $i=0, len=arLen; i<len; ++i ) {
	 	let $qty = parseInt(cart.item[i].qty);
	 	let $price = cart.item[i].price;
	 	let $amount = round(qty * price);
	 	cart.item[i].amount = amount;
	 	subtotal += amount;
	 	totalFilters += qty;
	};
	cart.discount = [];
	// calclulate multi filter discount
	if(totalFilters < 2)
	{
		if(cart.discount[0] && cart.discount[0].code == 'MFD')
			cart.discount.splice(0,1)
	} else {
		let $amt = (pricing.multiFilterDiscount.perAdditional) ? (totalFilters - 1) * pricing.multiFilterDiscount.amount : pricing.multiFilterDiscount.amount;
		let $replace = (cart.discount[0] && cart.discount[0].code == 'MFD') ? 1 : 0;
		cart.discount.splice(0,replace,{ code: 'MFD', desc:'Multiple Filter Discount', amount:amt });
	};
	// calculate other discounts from offer codes  { code:'megaplex', title:'Megaplex customer discount (5% off)', percent:5.0 },
	// TODO: Make this so it cannot be guessed because it is in the js.
	let $pdLen=pricing.discount.length;
	for ( let $i=0, len=pdLen; i<len; ++i ) {
			let $discount = pricing.discount[i];
			if(cart.offerCode.match(discount.code)) {
				let $amt = -1 * round(subtotal * (discount.percent / 100));
				cart.discount.push({ code: discount.code, desc:discount.title, amount: amt });
				break;
			}
	};
	// add discounts into the subtotal
	let $diLen=cart.discount.length;
	for ( let $i=0, len=diLen; i<len; ++i )
			subtotal += cart.discount[i].amount;
	cart.totalFilters = totalFilters;
	cart.subtotal = subtotal;
	cart.taxPercent = (pricing.tax[cart.state]) ? pricing.tax[cart.state] : 0;
	cart.tax = round(subtotal * (cart.taxPercent / 100));
	cart.deliveryTotal = round(cart.subtotal + cart.tax);
	cart.monthlySubTotal = round(cart.subtotal / cart.frequency);
	cart.monthlyTax = round(cart.monthlySubTotal * (cart.taxPercent / 100));
	cart.monthlyTotal = round(cart.monthlySubTotal + cart.monthlyTax);
	:)
